package View.Message;

import Service.ChangeDormAppService;
import Service.ServiceImpl.ChangeDormAppServiceImpl;
import Service.ServiceImpl.systemMsgServiceImpl;
import Service.systemMsgService;
import View.Index.Index_admin;
import View.Index.Index_user;
import View.apply.ChangeDormapp;
import domain.ChangeDormApp;
import domain.User;
import domain.systemMsg;

import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.Vector;

public class HistoryMsg extends JFrame {


    public HistoryMsg(User user){
        init();
        componentInit(user);
    }

    private Vector titlesv= new Vector();
    private Vector<Vector> datev= new Vector<>();
    JTable table=null;
    public void init(){
        this.setTitle("历史消息");
        this.setSize(1000,500);
        this.setResizable(true);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLayout(null);
    }
    public void componentInit(User user){
        String[] title={"id","标题","发送时间"};
        JButton button1 = new JButton();
        button1.setText("点击查看详情");
        button1.setBounds(100, 340, 175, 43);
        JButton button2 = new JButton();
        button2.setText("标记为未读");
        button2.setBounds(360, 340, 175, 43);
        JButton button3 = new JButton();
        button3.setText("退出");
        button3.setBounds(630, 340, 175, 43);

        JScrollPane panel = new JScrollPane();
        panel.setBounds(100, 5, 700, 300);

        systemMsgService service = new systemMsgServiceImpl();
        List<systemMsg> msgList = service.findHistoryMsgByUsername(user.getUsername());

        Object[][] neirong = new Object[msgList.size()][3];
        for (int i = 0; i < msgList.size(); i++) {
            systemMsg msg = msgList.get(i);
            neirong[i][0]=msg.getId();
            neirong[i][1]=msg.getTitle();
            neirong[i][2]=msg.getSendTime();
        }

        for (int i = 0; i < title.length; i++) {
            titlesv.add(title[i]);
        }

        for (int i = 0; i < neirong.length; i++) {
            Vector t = new Vector<>();
            for (int j = 0; j < neirong[i].length; j++) {
                t.add(neirong[i][j]);
            }
            datev.add(t);
        }
        MyTableModel tableModel = new MyTableModel();
        table = new JTable(tableModel);
        table.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        DefaultTableCellRenderer tcr = new DefaultTableCellRenderer();
        tcr.setHorizontalAlignment(SwingConstants.CENTER);
        table.setDefaultRenderer(Object.class, tcr);
        panel.setViewportView(table);


        this.add(panel);
        this.add(button1);
        this.add(button2);
        this.add(button3);

        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //获取选中条目
                int selectedRow = table.getSelectedRow();
                System.out.println("selectedRow:"+selectedRow);
                if(selectedRow==-1){
                    JOptionPane.showMessageDialog(new JFrame(),"请选择要删除的条目");
                    return;
                }

                String id = tableModel.getValueAt(selectedRow, 0).toString();

                systemMsg msg = service.findMsgByid(Integer.parseInt(id));
                new LookMsg(msg,user).setVisible(true);
            }
        });

        button2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //获取选中条目
                int selectedRow = table.getSelectedRow();
                System.out.println("selectedRow:"+selectedRow);
                if(selectedRow==-1){
                    JOptionPane.showMessageDialog(new JFrame(),"请选择要删除的条目");
                    return;
                }

                String id = tableModel.getValueAt(selectedRow, 0).toString();

                service.changeMsgIsread("false",Integer.parseInt(id));
                JOptionPane.showMessageDialog(new JFrame(),"修改完成");
                dispose();
                new HistoryMsg(user).setVisible(true);
            }
        });

        button3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
                if(user.getType().equals("admin")){
                    new Index_admin(user).setVisible(true);
                }else if(user.getType().equals("user")){
                    new Index_user(user).setVisible(true);
                }

            }
        });
    }

    private class MyTableModel extends AbstractTableModel {

        @Override
        public int getRowCount() {
            return datev.size();
        }

        @Override
        public int getColumnCount() {
            return titlesv.size();
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            return datev.get(rowIndex).get(columnIndex);
        }

        @Override
        public String getColumnName(int column) {
            return (String) titlesv.get(column);
        }

        @Override
        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return false;
        }
    }
}
